package com.tbit.main.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tbit.main.dao.core.CommonFunctionDao;
import com.tbit.main.pojo.AccountUser;
import com.tbit.main.pojo.CommonFunction;
import com.tbit.main.service.CommonFunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 14:52 2023/2/20
 */
@Service
public class CommonFunctionServiceImpl extends ServiceImpl<CommonFunctionDao, CommonFunction> implements
        CommonFunctionService {

    @Autowired
    private CommonFunctionDao commonFunctionDao;

    @Override
    public void add(List<String> list, AccountUser accountUser) {
        List<CommonFunction> commonFunctions = new ArrayList<>();
        for (String name : list) {
            CommonFunction commonFunction = new CommonFunction();
            commonFunction.setAddTime(new Date());
            commonFunction.setAccountUserId(accountUser.getAccountUserId());
            commonFunction.setAccountId(accountUser.getAccountId());
            commonFunction.setFunctionName(name);
            commonFunctions.add(commonFunction);
        }
        /**先删除，再添加*/
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("accountId", accountUser.getAccountId());
        remove(queryWrapper);
        if (!list.isEmpty()) {
            saveBatch(commonFunctions);
        }
    }

    @Override
    public List<CommonFunction> getUserFunction(Integer accountUserId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("accountUserId", accountUserId);
        List<CommonFunction> list = commonFunctionDao.selectList(queryWrapper);
        return list;
    }
}
